package com.blast.cn.work

import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.alibaba.fastjson.JSONArray
import com.blast.cn.event.UploadBlastEvent
import com.blast.cn.http.MainHttps
import com.blast.cn.http.OnUploadBlastCallback
import com.wxl.common.AppContext
import com.wxl.common.room.entity.BlastRecordEntity
import com.wxl.common.room.entity.LeiGuanBean
import com.wxl.common.room.entity.LeiGuanBlastEntity
import com.wxl.common.room.entity.LogEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.NetWorkUtils
import com.wxl.common.util.ObservableUtil
import org.greenrobot.eventbus.EventBus

class UploadBlastWork : Worker{

    private var position = 0
    private var isUploading = false

    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams)

    override fun doWork(): Result {
        if(!isUploading && NetWorkUtils.isNetWorkAvailable(AppContext.appContext.getApplication())) {
            findBlastRecord()
        }
        return Result.retry()
    }


    private fun findBlastRecord(){
        DBHelper.queryBlastRecordUnLoad(object : ObservableUtil.Companion.OnNextCallback<List<BlastRecordEntity>>(){
            override fun onNext(t: List<BlastRecordEntity>) {
                if(t.isNotEmpty()){
                    findUnUploadRecord(t)
                } else {
                    isUploading = false
                    doWork()
                }
            }
        })
    }

    /**
     * 找未上传的记录
     */
    private fun findUnUploadRecord(t: List<BlastRecordEntity>){
        for((i,item) in t.withIndex()){
            if(item.isUpload == 0){
                position = i
                uploadBlastRecord(item)
                return
            }
        }
        doWork()
    }

    /**
     * 上传爆破记录
     */
    private fun uploadBlastRecord(item:BlastRecordEntity){
        isUploading = true
        item.isUpload = 2
        DBHelper.updateBlastRecord(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
            override fun onNext(t: Boolean) {
                DBHelper.queryOperateLog(object : ObservableUtil.Companion.OnNextCallback<List<LogEntity>>(){
                    override fun onNext(t: List<LogEntity>) {
                        item.logList = arrayListOf()
                        item.logList.addAll(t)
                        DBHelper.queryBlastedLeiGuan(object : ObservableUtil.Companion.OnNextCallback<List<LeiGuanBlastEntity>>(){
                            override fun onNext(t: List<LeiGuanBlastEntity>) {
                                item.list = JSONArray.parseArray(JSONArray.toJSONString(t), LeiGuanBean::class.java) as ArrayList<LeiGuanBean>
                                upload(item)
                            }
                        }, item.blastId)
                    }
                })
            }
        },item)
        EventBus.getDefault().post(UploadBlastEvent(item))
    }



    private fun upload(item:BlastRecordEntity){
        MainHttps.uploadBlastRecord(item,object : OnUploadBlastCallback{
            override fun onUploadBlast(isOk: Boolean) {
                isUploading = false
                item.isUpload = if(isOk) 1 else 0
                item.blastRemark = if(isOk) "有效" else ""
                item.logList?.clear()
                item.list?.clear()
                if(isOk){
                    DBHelper.clearOperateLog()
                }
                DBHelper.updateBlastRecord(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                    override fun onNext(t: Boolean) {
                        EventBus.getDefault().post(UploadBlastEvent(item))
                        doWork()
                    }
                },item)
            }
        },false)
    }
}